﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using Guest_room.DAL;

namespace Guest_room.BLL
{
    public class GuestRoomService
    {
        dbConnection DB = new dbConnection();

        guest_roomEntities db = new guest_roomEntities();

        //业务逻辑步骤...


        /// <summary>
        /// 验证用户的身份信息
        /// </summary>
        /// <param name="userId">用户编号</param>
        /// <param name="password">用户密码</param>
        /// <param name="role">用户角色</param>
        /// <returns>如果验证成功返回 true，否则返回 false</returns>
        public bool ValidateUser(string userId, string password, string role)
        {
            var user = db.员工账户表
                .FirstOrDefault(u => u.用户编号 == userId && u.密码 == password && u.员工属性 == role);
            return user != null;
        }

        /// <summary>
        /// 获取用户的姓名
        /// </summary>
        /// <param name="userId">用户编号</param>
        /// <param name="role">用户角色</param>
        /// <returns>返回用户姓名</returns>
        public string GetUserName(string userId, string role)
        {
            if (role == "管理员")
            {
                var admin = db.管理员基本信息表
                    .FirstOrDefault(a => a.管理员编号 == userId);
                return admin?.管理员姓名;
            }
            else
            {
                var employee = db.员工基本信息表
                    .FirstOrDefault(e => e.员工编号 == userId);
                return employee?.员工姓名;
            }
        }

        /// <summary>
        /// 获取客房档次数据
        /// </summary>
        /// <returns>包含客房档次数据的SqlDataAdapter对象</returns>
        public SqlDataAdapter GetGuestRoomGrades()
        {
            DB.GetCn(); //建立连接
            string str = "select * from 客房档次表"; //定义 sql语句 的查询字符串
            SqlDataAdapter dagrade = new SqlDataAdapter(str, DB.cn); //执行并返回结果
            DB.CloseCn(); //关闭连接
            return dagrade;
        }


        /// <summary>
        /// 删除入住信息
        /// </summary>
        /// <param name="roomNumber">客房编号</param>
        /// <returns>是否删除成功</returns>
        public bool DeleteCheckInInfo(string roomNumber)
        {
            try
            {
                using (var db = new guest_roomEntities())
                {
                    // 删除用户购物车表中的数据
                    var cartItems = db.用户购物车表.Where(c => c.客房编号 == roomNumber).ToList();
                    db.用户购物车表.RemoveRange(cartItems);

                    // 更新客房档次表的空房数
                    var roomGradeIds = db.客房基本信息表.Where(r => r.客房编号 == roomNumber).Select(r => r.档次编号).ToList();
                    var roomGrades = db.客房档次表.Where(g => roomGradeIds.Contains(g.档次编号)).ToList();
                    foreach (var grade in roomGrades)
                    {
                        grade.空房数++;
                    }

                    // 更新客房基本信息表的客房状态为 '空闲'
                    var roomInfo = db.客房基本信息表.SingleOrDefault(r => r.客房编号 == roomNumber);
                    if (roomInfo != null)
                    {
                        roomInfo.客房状态 = "空闲";
                    }

                    // 保存所有更改
                    db.SaveChanges();

                    return true; // 操作成功返回 true
                }
            }
            catch (Exception ex)
            {
                // 处理异常情况，例如记录日志
                Console.WriteLine($"删除入住信息时出现异常：{ex.Message}");
                return false;
            }
        }


        /// <summary>
        /// 获取客户的详细信息和住宿信息
        /// </summary>
        /// <param name="customerName">客户姓名</param>
        /// <returns>包含客户详细信息的DataTable和包含客户住宿信息的DataTable</returns>
        public (DataTable, DataTable) GetCustomerInfo(string customerName)
        {
            var customerDetails = (from c in db.客户基本信息表
                                   join cs in db.客户住宿信息 on c.客户编号 equals cs.客户编号
                                   join e1 in db.员工基本信息表 on cs.入住经办员编号 equals e1.员工编号
                                   join e2 in db.员工基本信息表 on cs.退房经办员编号 equals e2.员工编号
                                   where c.客户姓名 == customerName
                                   select new
                                   {
                                       c.客户编号,
                                       c.客户姓名,
                                       c.客户性别,
                                       c.客户身份证,
                                       cs.客房编号,
                                       cs.入住日期,
                                       cs.退房日期,
                                       入住经办员 = e1.员工姓名,
                                       退房经办员 = e2.员工姓名,
                                       cs.预计入住时长,
                                       cs.房间金额,
                                       c.客户身份证照片
                                   }).ToList();

            DataTable dtCustomerDetails = new DataTable();
            dtCustomerDetails.Columns.Add("客户编号");
            dtCustomerDetails.Columns.Add("客户姓名");
            dtCustomerDetails.Columns.Add("客户性别");
            dtCustomerDetails.Columns.Add("客户身份证");
            dtCustomerDetails.Columns.Add("客房编号");
            dtCustomerDetails.Columns.Add("入住日期");
            dtCustomerDetails.Columns.Add("退房日期");
            dtCustomerDetails.Columns.Add("入住经办员");
            dtCustomerDetails.Columns.Add("退房经办员");
            dtCustomerDetails.Columns.Add("预计入住时长");
            dtCustomerDetails.Columns.Add("房间金额");
            dtCustomerDetails.Columns.Add("客户身份证照片");

            foreach (var detail in customerDetails)
            {
                dtCustomerDetails.Rows.Add(detail.客户编号, detail.客户姓名, detail.客户性别, detail.客户身份证, detail.客房编号, detail.入住日期, detail.退房日期, detail.入住经办员, detail.退房经办员, detail.预计入住时长, detail.房间金额, detail.客户身份证照片);
            }

            var stayInfo = db.客户住宿信息
                           .Where(cs => cs.客户姓名 == customerName)
                           .Select(cs => new
                           {
                               cs.客户编号,
                               cs.客房编号,
                               cs.入住日期,
                               cs.退房日期,
                               cs.预计入住时长,
                               cs.房间金额
                           }).ToList();

            DataTable dtStayInfo = new DataTable();
            dtStayInfo.Columns.Add("客户编号");
            dtStayInfo.Columns.Add("客房编号");
            dtStayInfo.Columns.Add("入住日期");
            dtStayInfo.Columns.Add("退房日期");
            dtStayInfo.Columns.Add("预计入住时长");
            dtStayInfo.Columns.Add("房间金额");

            foreach (var info in stayInfo)
            {
                dtStayInfo.Rows.Add(info.客户编号, info.客房编号, info.入住日期, info.退房日期, info.预计入住时长, info.房间金额);
            }

            return (dtCustomerDetails, dtStayInfo);
        }



        /// <summary>
        /// 提交订单，处理客房信息，包括保存客户身份证照片，添加客户和房间信息，并更新数据库和房间状态。
        /// </summary>
        /// <param name="ds">包含客户和房间信息的数据集。</param>
        /// <param name="dacustomer">客户数据的SqlDataAdapter。</param>
        /// <param name="daroom">房间数据的SqlDataAdapter。</param>
        /// <param name="clientId">客户编号。</param>
        /// <param name="clientName">客户姓名。</param>
        /// <param name="clientIdNumber">客户身份证号。</param>
        /// <param name="clientPhone">客户联系电话。</param>
        /// <param name="roomId">房间编号。</param>
        /// <param name="roomPrice">房间价格。</param>
        /// <param name="occupants">入住人数。</param>
        /// <param name="checkInDate">入住日期。</param>
        /// <param name="days">入住天数。</param>
        /// <param name="totalPrice">总价。</param>
        /// <param name="userId">处理入住的用户编号。</param>
        /// <param name="savedImagePath">保存客户身份证照片的路径。</param>
        public void ProcessGuestRoom(DataSet ds, SqlDataAdapter dacustomer, SqlDataAdapter daroom,
                                     string clientId, string clientName, string clientIdNumber,
                                     string clientPhone, string roomId, string roomPrice,
                                     int occupants, string checkInDate, int days, int totalPrice,
                                     string userId, string savedImagePath)
        {
            string file = SaveImageAndGetPath(savedImagePath);

            AddCustomerInfo(ds, clientId, clientName, clientIdNumber, clientPhone, file, roomId);
            AddRoomInfo(ds, clientId, clientName, roomId, roomPrice, occupants, checkInDate, days, totalPrice, userId);

            UpdateDatabase(dacustomer, daroom, ds);
            UpdateRoomStatus(roomId);
        }

        /// <summary>
        /// 保存客户身份证照片到指定路径，并返回新的文件路径。
        /// </summary>
        /// <param name="savedImagePath">保存客户身份证照片的路径。</param>
        /// <returns>保存后的文件路径。</returns>
        private string SaveImageAndGetPath(string savedImagePath)
        {
            string file = "";
            if (!string.IsNullOrEmpty(savedImagePath))
            {
                string targetFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Prod_Images");
                if (!Directory.Exists(targetFolderPath))
                {
                    Directory.CreateDirectory(targetFolderPath);
                }
                int fileNumber = 11;
                string newFileName;
                do
                {
                    newFileName = fileNumber.ToString().PadLeft(5, '0') + ".jpeg";
                    fileNumber++;
                } while (File.Exists(Path.Combine(targetFolderPath, newFileName)));
                string targetFilePath = Path.Combine(targetFolderPath, newFileName);
                File.Copy(savedImagePath, targetFilePath, true);
                file = "\\Prod_Images\\" + newFileName;
            }
            return file;
        }

        /// <summary>
        /// 向数据集添加客户信息。
        /// </summary>
        /// <param name="ds">包含客户信息的数据集。</param>
        /// <param name="clientId">客户编号。</param>
        /// <param name="clientName">客户姓名。</param>
        /// <param name="clientIdNumber">客户身份证号。</param>
        /// <param name="clientPhone">客户联系电话。</param>
        /// <param name="file">客户身份证照片的文件路径。</param>
        /// <param name="roomId">房间编号。</param>
        private void AddCustomerInfo(DataSet ds, string clientId, string clientName, string clientIdNumber,
                                     string clientPhone, string file, string roomId)
        {
            DataRow drEm = ds.Tables["pro_info"].NewRow();
            drEm["客户编号"] = clientId;
            drEm["客户姓名"] = clientName;
            drEm["客户性别"] = clientIdNumber.Length == 18 ? clientIdNumber.Substring(16, 1) == "1" ? "男" : "女" : "";
            drEm["联系电话"] = clientPhone;
            drEm["客户身份证"] = clientIdNumber;
            drEm["客户身份证照片"] = file;
            drEm["身份验证"] = "待审核";
            drEm["客房编号"] = roomId;
            ds.Tables["pro_info"].Rows.Add(drEm);
        }

        /// <summary>
        /// 向数据集添加房间信息。
        /// </summary>
        /// <param name="ds">包含房间信息的数据集。</param>
        /// <param name="clientId">客户编号。</param>
        /// <param name="clientName">客户姓名。</param>
        /// <param name="roomId">房间编号。</param>
        /// <param name="roomPrice">房间价格。</param>
        /// <param name="occupants">入住人数。</param>
        /// <param name="checkInDate">入住日期。</param>
        /// <param name="days">入住天数。</param>
        /// <param name="totalPrice">总价。</param>
        /// <param name="userId">处理入住的用户编号。</param>
        private void AddRoomInfo(DataSet ds, string clientId, string clientName, string roomId, string roomPrice,
                                 int occupants, string checkInDate, int days, int totalPrice, string userId)
        {
            DataRow drfo = ds.Tables["room_info"].NewRow();
            drfo["用户编号"] = clientId;
            drfo["用户姓名"] = clientName;
            drfo["客房编号"] = roomId;
            drfo["客房单价"] = roomPrice;
            drfo["入住人数"] = occupants;
            drfo["入住日期"] = checkInDate;
            drfo["入住经办员编号"] = userId;
            drfo["创建时间"] = DateTime.Now.ToString();
            drfo["入住天数"] = days;
            drfo["订单总价"] = totalPrice;
            drfo["结算状态"] = "未结算";
            ds.Tables["room_info"].Rows.Add(drfo);
        }

        /// <summary>
        /// 更新数据库中的客户和房间信息。
        /// </summary>
        /// <param name="dacustomer">客户数据的SqlDataAdapter。</param>
        /// <param name="daroom">房间数据的SqlDataAdapter。</param>
        /// <param name="ds">包含客户和房间信息的数据集。</param>
        private void UpdateDatabase(SqlDataAdapter dacustomer, SqlDataAdapter daroom, DataSet ds)
        {
            SqlCommandBuilder scb = new SqlCommandBuilder(dacustomer);
            SqlCommandBuilder scb1 = new SqlCommandBuilder(daroom);
            dacustomer.Update(ds, "pro_info");
            daroom.Update(ds, "room_info");
        }

        /// <summary>
        /// 更新房间状态为“占用”，并减少空房数量。
        /// </summary>
        /// <param name="roomId">房间编号。</param>
        private void UpdateRoomStatus(string roomId)
        {
            // 查找房间
            var room = db.客房基本信息表.SingleOrDefault(r => r.客房编号 == roomId);
            if (room != null)
            {
                // 更新房间状态
                room.客房状态 = "占用";

                // 查找房间的档次
                var roomCategory = db.客房档次表.SingleOrDefault(rc => rc.档次编号 == room.档次编号);
                if (roomCategory != null)
                {
                    // 更新空房数量
                    roomCategory.空房数 -= 1;
                }

                // 保存更改
                db.SaveChanges();
            }
        }



        /// <summary>
        /// 获取指定档次编号的客房单价范围。
        /// </summary>
        /// <param name="gradeId">档次编号。</param>
        /// <returns>返回该档次编号的客房单价范围，格式为 "minPrice - maxPrice"。</returns>
        public string GetPriceRangeByGrade(string gradeId)
        {
            using (var db = new guest_roomEntities())
            {
                var maxPriceQuery = from r in db.客房基本信息表
                                    where r.档次编号 == gradeId
                                    select r.客房单价;

                var minPriceQuery = from r in db.客房基本信息表
                                    where r.档次编号 == gradeId
                                    select r.客房单价;

                var maxPrice = maxPriceQuery.Max()?.ToString();
                var minPrice = minPriceQuery.Min()?.ToString();

                return minPrice + " - " + maxPrice;
            }
        }
        /// <summary>
        /// 更改员工密码。
        /// </summary>
        /// <param name="userId">用户编号</param>
        /// <param name="oldPassword">原密码</param>
        /// <param name="newPassword">新密码</param>
        /// <param name="confirmPassword">确认新密码</param>
        /// <returns>操作结果信息</returns>
        public string ChangeEmployeePassword(string userId, string oldPassword, string newPassword, string confirmPassword)
        {
            // 建立实体框架上下文
            using (var db = new guest_roomEntities())
            {
                // 查询用户账户
                var employee = db.员工账户表.FirstOrDefault(e => e.用户编号 == userId && e.密码 == oldPassword);

                if (employee == null)
                {
                    return "原密码错误，请重新输入！";
                }

                if (string.IsNullOrEmpty(newPassword) || string.IsNullOrEmpty(confirmPassword))
                {
                    return "请输入新密码！";
                }

                if (newPassword != confirmPassword)
                {
                    return "两次密码不一致！";
                }

                // 更新密码
                employee.密码 = newPassword;

                try
                {
                    db.SaveChanges(); // 保存更改
                    return "密码修改成功！";
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message); // 可以记录或处理异常
                    return "密码修改失败！";
                }
            }
        }



        /// <summary>
        /// 获取用户购物车的内容。
        /// </summary>
        /// <param name="userId">用户ID。</param>
        /// <returns>包含用户购物车内容的SqlDataAdapter。</returns>
        public SqlDataAdapter GetUserCart(string userId)
        {
            DB.GetCn();
            string str = "select * from 用户购物车表 where 入住经办员编号='" + userId + "' and 结算状态='未结算'";
            SqlDataAdapter Dachart = new SqlDataAdapter(str, DB.cn);
            DB.cn.Close();
            return Dachart;
        }

        /// <summary>
        /// 获取用户购物车中的项目列表。
        /// </summary>
        /// <param name="userId">用户ID。</param>
        /// <returns>包含用户购物车项目的列表。</returns>
        public List<用户购物车表> GetUserShoppingCartItems(string userId)
        {
            return db.用户购物车表
                     .Where(c => c.入住经办员编号 == userId && c.结算状态 == "未结算")
                     .ToList();
        }


        /// <summary>
        /// 通过更新房间状态和删除用户购物车中的项目来清空购物车。
        /// </summary>
        /// <param name="roomNumbers">要从购物车中清空的房间编号数组。</param>
        /// <param name="userId">购物车所属用户的用户ID。</param>
        /// <exception cref="Exception">当购物车为空或数据库操作出错时抛出异常。</exception>
        public void ClearShoppingCart(string[] roomNumbers, string userId)
        {
            using (var db = new guest_roomEntities())
            {
                foreach (var roomNo in roomNumbers)
                {
                    var roomInfo = db.客房基本信息表.FirstOrDefault(r => r.客房编号 == roomNo);
                    if (roomInfo != null)
                    {
                        // 更新客房状态为 "空闲"
                        roomInfo.客房状态 = "空闲";
                        // 查找对应的客房档次表记录
                        var roomGrade = db.客房档次表.FirstOrDefault(g => g.档次编号 == roomInfo.档次编号);
                        if (roomGrade != null)
                        {
                            // 更新空房数
                            roomGrade.空房数 += 1;
                        }
                    }
                }

                // 查找并删除用户购物车表中的记录
                var recordsToDelete = db.用户购物车表
                                        .Where(c => c.入住经办员编号 == userId)
                                        .ToList();

                if (recordsToDelete.Any())
                {
                    // 删除找到的记录
                    db.用户购物车表.RemoveRange(recordsToDelete);
                }

                // 保存更改到数据库
                db.SaveChanges();

            }
        }

        /// 更新用户购物车中的房间信息。
        /// </summary>
        /// <param name="roomNumber">房间编号。</param>
        /// <param name="peoples">入住人数。</param>
        /// <param name="roomtime">入住日期。</param>
        /// <param name="currentStayDays">入住天数。</param>
        /// <param name="roomupdatetime">更新时间。</param>
        /// <param name="roomtotalprice">订单总价。</param>
        public void UpdateUserCart(string roomNumber, string userId, int peoples, string roomtime, 
            int currentStayDays, string roomupdatetime, int roomtotalprice)
        {
            using (var db = new guest_roomEntities())
            {
                // 获取要更新的记录
                var cartItem = db.用户购物车表.FirstOrDefault(c => c.客房编号 == roomNumber && c.入住经办员编号 == userId);

                // 如果找到了对应的记录
                if (cartItem != null)
                {
                    cartItem.入住人数 = peoples;
                    cartItem.入住日期 = roomtime;
                    cartItem.入住天数 = currentStayDays;
                    cartItem.更新时间 = roomupdatetime;
                    cartItem.订单总价 = roomtotalprice;

                    // 保存更改
                    db.SaveChanges();
                }
                else
                {
                    // 处理找不到记录的情况
                    throw new Exception("未找到匹配的记录");
                }
            }
        }


        /// <summary>
        /// 获取员工编号的后两位，并递增1后返回
        /// </summary>
        /// <returns>新的员工编号后两位</returns>
        public string GetNum()
        {
            // 使用实体框架从数据库获取员工基本信息表的最大员工编号
            var maxEmployee = db.员工基本信息表
                .OrderByDescending(e => e.员工编号)
                .FirstOrDefault();

            string num = maxEmployee != null ? maxEmployee.员工编号 : "E000";

            // 提取编号中的数值部分并递增
            string numericPart = num.Substring(1); // 假设编号格式为 "E999"
            int incrementedNum = int.Parse(numericPart) + 1;

            // 格式化为两位数，如果超过两位则取后两位
            string newNum = incrementedNum.ToString("D2");

            return newNum;
        }

        /// <summary>
        /// 注册用户并返回注册结果消息
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="confirmPassword">确认密码</param>
        /// <param name="userType">用户类型</param>
        /// <returns>注册结果消息</returns>
        public string RegisterUser(string username, string password, string confirmPassword, string userType)
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return "请输入用户名或密码！";
            }

            if (password != confirmPassword)
            {
                return "两次密码不一致，请重新输入！";
            }

            var userExists = db.员工账户表.Any(u => u.用户编号 == username);
            if (userExists)
            {
                return "此用户已存在";
            }

            var newUser = new 员工账户表
            {
                用户编号 = username,
                密码 = password,
                员工属性 = userType
            };
            db.员工账户表.Add(newUser);
            db.SaveChanges();
            return "注册成功！";
        }


        /// <summary>
        /// 检查部门编号或部门名称是否存在
        /// </summary>
        /// <param name="departmentId">部门编号</param>
        /// <param name="departmentName">部门名称</param>
        /// <returns>如果部门编号或部门名称存在，返回true；否则返回false</returns>
        public bool IsDepartmentExist(string departmentId, string departmentName)
        {
            var isExist = db.部门基本信息表.Any(d => d.部门编号 == departmentId || d.部门名称 == departmentName);
            return isExist;
        }

        /// <summary>
        /// 检查部门下是否有员工或管理员
        /// </summary>
        /// <param name="departmentId">部门编号</param>
        /// <returns>如果部门下有员工或管理员，返回true；否则返回false</returns>
        public bool HasStaffOrAdmin(string departmentId)
        {
            var hasStaff = db.员工基本信息表.Any(e => e.员工部门 == departmentId);
            var hasAdmin = db.管理员基本信息表.Any(a => a.管理员部门 == departmentId);
            return hasStaff || hasAdmin;
        }


        /// <summary>
        /// 获取所有客房订单
        /// </summary>
        /// <returns>客房订单列表</returns>
        public List<客房订单表> GetAllOrders()
        {
            return db.客房订单表.ToList();
        }

        /// <summary>
        /// 更新订单状态
        /// </summary>
        /// <param name="orderId">订单编号</param>
        /// <param name="status">新的订单状态</param>
        public void UpdateOrderStatus(int orderId, string status)
        {
            var order = db.客房订单表.SingleOrDefault(o => o.订单编号 == orderId);
            if (order != null)
            {
                order.订单状态 = status;
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 根据订单编号获取订单详细信息
        /// </summary>
        /// <param name="orderId">订单编号</param>
        /// <returns>订单详细信息</returns>
        public DataTable GetOrderDetails(int orderId)
        {
            var query = from o in db.客房订单表
                        join u in db.客户基本信息表 on o.客户编号 equals u.客户编号
                        join r in db.客房基本信息表 on o.客房编号 equals r.客房编号
                        join c in db.客房档次表 on r.档次编号 equals c.档次编号
                        where o.订单编号 == orderId
                        select new
                        {
                            o.订单编号,
                            o.入住经办员编号,
                            o.客房编号,
                            r.客房单价,
                            c.客房档次,
                            r.客房电话,
                            o.入住天数,
                            o.入住人数,
                            o.入住时间,
                            o.订单总价,
                            u.客户编号,
                            u.客户姓名,
                            u.联系电话,
                            u.客户身份证,
                            u.身份验证,
                            u.客户性别,
                            u.客户身份证照片
                        };

            DataTable dt = new DataTable();
            dt.Columns.Add("订单编号");
            dt.Columns.Add("入住经办员编号");
            dt.Columns.Add("客房编号");
            dt.Columns.Add("客房单价");
            dt.Columns.Add("客房档次");
            dt.Columns.Add("客房电话");
            dt.Columns.Add("入住天数");
            dt.Columns.Add("入住人数");
            dt.Columns.Add("入住时间");
            dt.Columns.Add("订单总价");
            dt.Columns.Add("客户编号");
            dt.Columns.Add("客户姓名");
            dt.Columns.Add("联系电话");
            dt.Columns.Add("客户身份证");
            dt.Columns.Add("身份验证");
            dt.Columns.Add("客户性别");
            dt.Columns.Add("客户身份证照片");

            foreach (var item in query)
            {
                DataRow row = dt.NewRow();
                row["订单编号"] = item.订单编号;
                row["入住经办员编号"] = item.入住经办员编号;
                row["客房编号"] = item.客房编号;
                row["客房单价"] = item.客房单价;
                row["客房档次"] = item.客房档次;
                row["客房电话"] = item.客房电话;
                row["入住天数"] = item.入住天数;
                row["入住人数"] = item.入住人数;
                row["入住时间"] = item.入住时间;
                row["订单总价"] = item.订单总价;
                row["客户编号"] = item.客户编号;
                row["客户姓名"] = item.客户姓名;
                row["联系电话"] = item.联系电话;
                row["客户身份证"] = item.客户身份证;
                row["身份验证"] = item.身份验证;
                row["客户性别"] = item.客户性别;
                row["客户身份证照片"] = item.客户身份证照片;
                dt.Rows.Add(row);
            }

            return dt;
        }

        /// <summary>
        /// 根据客房档次获取客房信息
        /// </summary>
        /// <param name="roomGrade">客房档次</param>
        /// <returns>客房信息列表</returns>
        public IEnumerable<object> GetRoomsByGrade(string roomGrade)
        {
            var rooms = from r in db.客房基本信息表
                        where r.档次编号 == (from g in db.客房档次表
                                         where g.客房档次 == roomGrade
                                         select g.档次编号).FirstOrDefault()
                        select new
                        {
                            r.客房编号,
                            r.档次编号,
                            r.客房单价,
                            r.客房状态,
                            r.客房电话,
                            r.客房图片
                        };
            return rooms.ToList();
        }

        /// <summary>
        /// 根据客房档次获取档次编号
        /// </summary>
        /// <param name="roomGrade">客房档次</param>
        /// <returns>档次编号</returns>
        public string GetGradeIdByGrade(string roomGrade)
        {
            return (from r in db.客房档次表
                    where r.客房档次 == roomGrade
                    select r.档次编号).FirstOrDefault();
        }
        /// <summary>
        ///  返回日志表信息
        /// </summary>
        public class LogModel
        {
            public int 日志编号 { get; set; }
            public string 操作员编号 { get; set; }
            public string 操作员 { get; set; }
            public string 操作类型 { get; set; }
            public DateTime 操作时间 { get; set; }
            public string 操作表 { get; set; }
            public string 操作内容 { get; set; }
        }
        public IEnumerable<LogModel> GetLogs()
        {
            return db.日志表.Select(log => new LogModel
            {
                日志编号 = log.日志编号,
                操作员编号 = log.操作员编号,
                操作员 = log.操作员,
                操作类型 = log.操作类型,
                操作时间 = (DateTime)(log.操作时间),
                操作表 = log.操作表,
                操作内容 = log.操作内容
            }).ToList();
        }
    }
}
